package com.ccx.coachsite.ui.manage;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.ui.own.OwerMainBase;

public class MangeGroupInfoView extends OwerBaseView {

	private List<ManageGroupInfo> groups;
	private ManageGroupInfo selectedGroup;
	private List<ManageClientInfo> clients, selectedClients;

	private Spinner spinner;
	private Button btnMove;
	private ProgressDialog waitingDialog;

	public MangeGroupInfoView(Context context, OwerMainBase superView,
			int viewId) {
		super(context, superView);
	}

	@SuppressWarnings("unchecked")
	public MangeGroupInfoView(Context context, OwerMainBase superView,
			Object cinfo) {
		super(context, superView);
		List<Object> args = (List<Object>) cinfo;
		List<ManageGroupInfo> source = (List<ManageGroupInfo>) args.get(0);
		selectedGroup = (ManageGroupInfo) args.get(1);

		((TextView) findViewById(R.id.client_group_list_title))
				.setText(selectedGroup.getName());

		selectedClients = new ArrayList<ManageClientInfo>();
		groups = new ArrayList<ManageGroupInfo>();
		for (ManageGroupInfo group : source) {
			if (!group.equals(selectedGroup))
				groups.add(group);
		}
		spinner = (Spinner) findViewById(R.id.client_group_spinner);
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
				mContext, android.R.layout.simple_spinner_item);
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for (ManageGroupInfo group : groups) {
			spinnerAdapter.add(group.getName());
		}
		spinner.setAdapter(spinnerAdapter);
		if (spinnerAdapter.getCount() <= 0) {
			TextView txtHint = (TextView) findViewById(R.id.client_group_noitem_hint);
			txtHint.setVisibility(View.VISIBLE);
			spinner.setEnabled(false);
		}

		btnMove = (Button) findViewById(R.id.client_group_move_ok);
		btnMove.setOnClickListener(btnMoveClickListener);
		loadData();
	}

	@Override
	protected boolean onLoadData() {
		clients = ManageProvider.getInstance().loadClients(mContext,
				selectedGroup.getId());
		if (clients != null)
			return true;
		return false;
	}

	@Override
	protected void doRefreshDataSuccess() {
		super.doRefreshDataSuccess();
		ListView clientList = (ListView) findViewById(R.id.client_group_detail_list);
		ClientsAdapter adapter = new ClientsAdapter();
		clientList.setAdapter(adapter);
	}

	private void onMoveSuccess() {
		if (waitingDialog != null) {
			waitingDialog.dismiss();
			waitingDialog = null;
		}
		isRefresh = true;
		Toast.makeText(mContext, "已移动至指定分组", Toast.LENGTH_SHORT).show();
		loadData();
	}

	private void onMoveFail() {
		if (waitingDialog != null) {
			waitingDialog.dismiss();
			waitingDialog = null;
		}
		if (TextUtils.isEmpty(ManageProvider.getInstance()
				.getLastErrorMessage())) {
			Toast.makeText(mContext, "移动失败，请重试", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(mContext,
					ManageProvider.getInstance().getLastErrorMessage(),
					Toast.LENGTH_SHORT).show();
		}
	}

	OnCheckedChangeListener selectedCheckListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			ManageClientInfo client = (ManageClientInfo) buttonView.getTag();
			if (isChecked)
				selectedClients.add(client);
			else
				selectedClients.remove(client);
		}
	};

	OnClickListener btnMoveClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
//			if (selectedClients.size() <= 0) {
//				Toast.makeText(mContext, "请选择要移动分组的用户", Toast.LENGTH_SHORT)
//						.show();
//				return;
//			}
//			if (spinner.getSelectedItemPosition() == Spinner.INVALID_POSITION) {
//				Toast.makeText(mContext, "请选择分组", Toast.LENGTH_SHORT).show();
//				return;
//			}
//
//			ManageGroupInfo group = (ManageGroupInfo) groups.get(spinner
//					.getSelectedItemPosition());
//			waitingDialog = new ProgressDialog(mContext);
//			waitingDialog.setMessage("请稍候");
//			waitingDialog.show();
//			MoveTask task = new MoveTask();
//			task.execute(group.getId());
		}
	};

	class ClientsAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return clients.size();
		}

		@Override
		public Object getItem(int position) {
			return clients.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view = convertView;
			
			ManageClientItemCache cache = null;
			if (null == view) {
				view = View.inflate(mContext, R.layout.client_group_list_item, null);
				cache = new ManageClientItemCache(view);
				view.setTag(cache);
			} else {
				cache = (ManageClientItemCache) view.getTag();
			}
			
			ManageClientInfo client = clients.get(position);
			
			cache.chkSelected.setTag(client);
			cache.chkSelected.setOnCheckedChangeListener(selectedCheckListener);

			if ( selectedClients!=null && selectedClients.contains(client) ){
				cache.chkSelected.setChecked(true);
			}else{
				cache.chkSelected.setChecked(false);
			}
			
			cache.txtName.setText(client.getMobile());
			cache.txtFlow.setText(String.valueOf(client.getFlow()));
			
			return view;
		}
	}

	class ManageClientItemCache {
		
		public TextView txtName;
		public TextView txtFlow;
		public CheckBox chkSelected;
		
		public ManageClientItemCache(View view) {
			
			txtName = (TextView) view.findViewById(R.id.client_group_list_item_name);
			txtFlow = (TextView) view.findViewById(R.id.client_group_list_item_flow);
			chkSelected = (CheckBox) view.findViewById(R.id.client_group_list_item_check);
			
		}
	}
	
	
	class MoveTask extends AsyncTask<Integer, Integer, Boolean> {

		@Override
		protected Boolean doInBackground(Integer... params) {
//			if (selectedGroup.getId() == 0)
//				return ManageProvider.getInstance().addClientsToGroup(mContext,
//						selectedClients, params[0]);
//			else if (params[0] == 0)
//				return ManageProvider.getInstance().removeClients(mContext,
//						selectedClients, selectedGroup.getId());
//			else
//				return ManageProvider.getInstance().moveClients(mContext,
//						selectedClients, params[0], selectedGroup.getId());
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (result)
				onMoveSuccess();
			else
				onMoveFail();
		}
	}
}
